package com.ocm.commerceinteraction.ui;


import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;


import com.ocm.commerceinteraction.R;
import com.ocm.commerceinteraction.util.view.ProgressWebView;
import com.ocm.commerceinteraction.util.view.percentsupport.PercentRelativeLayout;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.ViewById;


public class ChatWebActivity extends Activity {

    private static final String TAG = "ChatWebActivity";
    ProgressWebView adWebview;
    PercentRelativeLayout backBtn;
    TextView topTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_chatweb);
        adWebview = (ProgressWebView)findViewById(R.id.ad_webview);
        backBtn = (PercentRelativeLayout)findViewById(R.id.back_btn);
        topTitle = (TextView)findViewById(R.id.toptitle_title);
        Intent intent=getIntent();
        String url=intent.getStringExtra("url");
        loadData(url);
    }
    //加载数据
    public void loadData(String url) {
        backBtn.setOnClickListener(
            new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   if(adWebview.canGoBack())adWebview.goBack();
                   else finish();
               }
           }
        );
        //       String url = "http://www.jq22.com/yanshi1845";
        WebSettings settings = adWebview.getSettings();
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(true);
        adWebview.setVerticalScrollBarEnabled(true);
        adWebview.setHorizontalScrollBarEnabled(true);
        adWebview.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                topTitle.setText(view.getTitle());
            }
        });

        if(url.indexOf("http")<0)adWebview.loadUrl("http://"+url);
        else adWebview.loadUrl(url);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if(keyCode== KeyEvent.KEYCODE_BACK)
        {
            if(adWebview.canGoBack())
            {
                adWebview.goBack();//返回上一页面
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
    private ValueCallback<Uri> mUploadMessage;
    ValueCallback<Uri[]> mUploadCallbackAboveL;
    private final static int FILECHOOSER_RESULTCODE = 1;

    // 一下2个方法解决  onShowFileChooser只加载一次的问题
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e(TAG, "onActivityResult: ");
        if(requestCode==FILECHOOSER_RESULTCODE)
        {
            if (null == mUploadMessage && null == mUploadCallbackAboveL) return;
            Uri result = data == null || resultCode != Activity.RESULT_OK ? null : data.getData();
            if (mUploadCallbackAboveL != null) {
                onActivityResultAboveL(requestCode, resultCode, data);
            }
            else  if (mUploadMessage != null) {
                mUploadMessage.onReceiveValue(result);
                mUploadMessage = null;
            }
        }
    }


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void onActivityResultAboveL(int requestCode, int resultCode, Intent data) {
        Log.e(TAG, "onActivityResultAboveL: ");
        if (requestCode != FILECHOOSER_RESULTCODE
                || mUploadCallbackAboveL == null) {
            return;
        }

        Uri[] results = null;
        if (resultCode == Activity.RESULT_OK) {
            if (data == null) {
            } else {
                String dataString = data.getDataString();
                ClipData clipData = data.getClipData();

                if (clipData != null) {
                    results = new Uri[clipData.getItemCount()];
                    for (int i = 0; i < clipData.getItemCount(); i++) {
                        ClipData.Item item = clipData.getItemAt(i);
                        results[i] = item.getUri();
                    }
                }

                if (dataString != null)
                    results = new Uri[]{Uri.parse(dataString)};
            }
        }
        mUploadCallbackAboveL.onReceiveValue(results);
        mUploadCallbackAboveL = null;
        return;
    }


    @Override
    public void onResume() {
        super.onResume();
       // loadData();
    }
}
